/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.PlatformUI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import es.upm.dit.gsi.eclipse.jadex.navigator.SubChild;

/**
 * This page allows to add new imports, and modify or delete existing ones.
 *  
 *  @author Mario Moreno Zarco
 */

public class AddImportWizardPage1 extends WizardPage {
	private Text importText;

	
	private Button addButton;
	private Button deleteButton;
	
	private static Table importsTable;
	private static ArrayList<String> importsList;
	
	
	/**
	 * Constructor for AddBeliefWizardPage1
	 * 
	 * @param pageName
	 */
	public AddImportWizardPage1(ISelection selection, ArrayList<String> importsList) {
		super("wizardPage");
		setTitle("Add Import/set Wizard");
		setDescription("Page 1: imports");
		this.importsList = importsList;
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout(2, true);
		container.setLayout(layout);
		layout.horizontalSpacing = 50;
		layout.verticalSpacing = 4;	
		PlatformUI.getWorkbench().getHelpSystem().setHelp(container, "es.upm.dit.gsi.eclipse.jadex.adfmanager.addBeliefWizard");
		
		GridData gd1 = new GridData(SWT.FILL, SWT.TOP, true, false,1,1);
		GridData gd2 = new GridData(SWT.FILL, SWT.TOP, true, false,2,1);
		
		
		Label label1 = new Label(container, SWT.NULL);
		label1.setText("Content: ");
		importText = new Text(container, SWT.BORDER | SWT.SINGLE);
		importText.setLayoutData(gd1);
		importText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		
		
		Label separator1 = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		separator1.setLayoutData(gd2);
		
		addButton = new Button(container,SWT.PUSH);
		addButton.setText("Add");
		addButton.setLayoutData(gd1);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if(importsTable.getSelection().length > 0){
					addButtonPushed(true);
				}
				else{
					addButtonPushed(false);
				}
			}
		});
		
		deleteButton = new Button(container,SWT.PUSH);
		deleteButton.setText("Delete");
		deleteButton.setLayoutData(gd1);
		deleteButton.setEnabled(false);
		deleteButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				deleteButtonPushed();
			}
		});
		
		importsTable = new Table (container, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
		importsTable.setLinesVisible (true);
		importsTable.setHeaderVisible (true);
		GridData gd = new GridData(SWT.FILL, SWT.TOP, true, false,2,1);
    	gd.heightHint = 200;
    	importsTable.setLayoutData(gd);
    	
    	TableColumn column = new TableColumn (importsTable, SWT.NONE);
    	column.setText(new String ("Content"));
    	importsTable.getColumn(0).pack();
    	importsTable.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				importSelected();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
    	
    	for(String imp : importsList){
    		TableItem item  = new TableItem(importsTable, SWT.NONE);
			item.setText(imp);
    	}
    	
		initialize();
		dialogChanged();
		setControl(container);
			
	}
	
	/*
	 * This method is called when the add button is pushed.
	 * It adds a new import or modify an existing one depending on 
	 * the desired operation.
	 */
	
	private void addButtonPushed(Boolean modify){
		if(modify){
			TableItem item = importsTable.getSelection()[0];
			item.setText(0,importText.getText());;
		}
		else{
			TableItem item  = new TableItem(importsTable, SWT.NONE);
			item.setText(importText.getText());				
		}
		clearForm();
	}
	
	/*
	 * This method is called when the delete button is pushed,
	 *  it deletes the selected import
	 */
	private void deleteButtonPushed(){
		importsTable.remove(importsTable.getSelectionIndices());
		clearForm();
	}
	
	/*
	 * This method is called  import is selected in the table
	 */
	private void importSelected(){
		TableItem item = importsTable.getSelection()[0];
		importText.setText(item.getText(0));;		
		deleteButton.setEnabled(true);
		addButton.setText("Modify");
	}
	
	/*
	 * This method clears all the controls text
	 */
	private void clearForm(){
		importText.setText("");
		importsTable.deselectAll();
		addButton.setText("Add");
		deleteButton.setEnabled(false);
	}
	

	/*
	 * This method is called when the text of any control changes
	 * or set them to the default selection
	 */
	private void dialogChanged() {
		
		if(importsTable.getItems().length == 0){
			updateStatus("At least a fact must be specified");
			return;
		}
		
		for (TableItem item : importsTable.getItems()){
			String[] content = new String[importsTable.getColumnCount()];
				content[0] = item.getText(0);
				if(content[0].length() == 0){
					updateStatus("At least a name must be specified");
					return;
				}
		}
		
	updateStatus(null);
	}

	/*
	 * This method is called in order to initialize the controls status
	 */
	private void initialize() {
		enabledPropertiesChanged();
	}
	
	/*
	 * This method is called when any 'enable' checkbox is checked
	 */
	private void enabledPropertiesChanged(){
		
	}
	
	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}
	
	/**
	 * This method gives access to the importsList
	 * @returns ArrayList<String> importList
	 */
	
	public static ArrayList<String> getImportsList(){
		
		ArrayList<String> newImportsList = new ArrayList<String>();
		
		for(TableItem item : importsTable.getItems()){
			 newImportsList.add(item.getText(0));
			}
		return newImportsList;
	}
}

